import { Component, _decorator, NodeEventType, EventTouch, v3, CCFloat, CCBoolean, Vec3, v2, Vec2 } from 'cc';
const {ccclass, property} = _decorator;
@ccclass
export default class ScaleControl extends Component {
    @property({type:Component,displayName:'目标'})
    private content: Component = null;
    @property({type:CCBoolean,displayName:'是否限制缩放'})
    private limit_scale: boolean = false;
    @property({type:CCFloat,visible(){return this.limit_scale;}})
    private max_scale: number = 2;
    @property({type:CCFloat,visible(){return this.limit_scale;}})
    private min_scale: number = 0.5;
    /*@property(Component)
    private circle0: Component = null;
    @property(Component)
    private circle1: Component = null;*/
    /*@property(Component)
    private remarkpoint: Component = null;
    @property(Component)
    private scale_point: Component = null;*/
    private old_scale: number = 1;
    //private scale_center_point: Vec3 = v3(0,0,0);
    protected onLoad(): void {
        //
        if(this.content){
            this.old_scale = this.content.node.scale.x;
        }
    }
    protected onEnable(): void {
        this.node.on(NodeEventType.TOUCH_START, this.onTouchStart, this);
        this.node.on(NodeEventType.TOUCH_MOVE, this.onScaleChange, this);
    }
    protected onDisable(): void {
        this.node.off(NodeEventType.TOUCH_START, this.onTouchStart, this);
        this.node.off(NodeEventType.TOUCH_MOVE, this.onScaleChange, this);
    }

    private onScaleChange(e:EventTouch): void {
        //console.log(e.getTouches());
        //console.log(e.touch.getDelta());
        const touchs = e.getTouches();
        if(touchs.length==2){
            //获取首次触点
            const fp0=touchs[0].getUIStartLocation();
            const fp1=touchs[1].getUIStartLocation();
            //获取当前触点
            const p0=touchs[0].getUILocation();
            const p1=touchs[1].getUILocation();
            //
            const scale_center_point=v3((p0.x+p1.x)/2,(p0.y+p1.y)/2,0);
            //this.scale_point.node.setWorldPosition(scale_center_point);
            //设置调试圆点位置
            //this.circle0.node.setWorldPosition(v3(p0.x,p0.y,0))
            //this.circle1.node.setWorldPosition(v3(p1.x,p1.y,0))
            //计算两点间距离
            const fdistance=Math.sqrt(Math.pow(fp0.x-fp1.x,2)+Math.pow(fp0.y-fp1.y,2));
            const distance=Math.sqrt(Math.pow(p0.x-p1.x,2)+Math.pow(p0.y-p1.y,2));
            //计算缩放比例
            const scale=(distance-fdistance)*0.002;
            let newScale=this.old_scale+scale;
            if(this.limit_scale){
                if(newScale>this.max_scale){
                    newScale=this.max_scale;
                }else if(newScale<this.min_scale){
                    newScale=this.min_scale;
                }
            }
            this.content.node.setScale(v3(newScale,newScale,1))//=this.old_scale*distance/1000;
        }
    }
    private onTouchStart(e:EventTouch) {
        //console.log(e.getTouches());
        /*const touchs = e.getTouches();
        if(touchs.length==2){
            //获取当前触点
            const p0=touchs[0].getUILocation();
            const p1=touchs[1].getUILocation();
            const scale_center_point=v3((p0.x+p1.x)/2,(p0.y+p1.y)/2,0);
            this.remarkpoint.node.setWorldPosition(scale_center_point);
            this.remarkpoint.node.setSiblingIndex(this.content.node.children.length-1)
            
            //this.scale_point.node.setWorldPosition(this.scale_center_point);
            //设置调试圆点位置
            //this.circle0.node.setWorldPosition(v3(p0.x,p0.y,0))
            //this.circle1.node.setWorldPosition(v3(p1.x,p1.y,0))
        }*/
        this.old_scale=this.content.node.scale.x;
    }
}